const { UNKNOWN_ERROR } = require("../config/error-constants");
const labelService = require("../service/label.service");
const { to } = require("../utils");

class LabelController {
    async create(ctx, next) {
        const { name } = ctx.request.body;
        const { id } = ctx.user;
        const [error, resp] = await to(labelService.create(name, id));
        if (error) {
            return ctx.app.emit("error", UNKNOWN_ERROR, ctx);
        }

        ctx.body = {
            code: 0,
            message: "创建标签成功~",
        };
    }

    async list(ctx, next) {
        const [error, resp] = await to(labelService.list());
        if (error) {
            return ctx.app.emit("error", UNKNOWN_ERROR, ctx);
        }

        ctx.body = {
            code: 0,
            data: resp,
        };
    }

    async removeLabelById() {
        const { labelId } = ctx.params;
        const [error, resp] = await to(labelService.removeLabelById(labelId));
        if (error) {
            return ctx.app.emit("error", UNKNOWN_ERROR, ctx);
        }
        ctx.body = {
            code: 0,
            message: "删除标签成功~",
        };
    }
}

module.exports = new LabelController();
